#include <iostream>
#include <cstring>
#include <iomanip>
#include <stdlib.h>
#include <ctime>
using namespace std;
//������Ʒ������
class goods
{
protected:
    char name[60];        //��Ʒ����
    char price[20];       //��Ʒ�۸�
    char adress[60];      //��Ʒ����
    char mill[60];        //��������
    int id;               //���
    goods *Mynext;        //ָ����һ������
public:
    goods();             //��ʼ������
    goods(char pname[],char pprice[],char padress[],char pmill[],int pid);//��ʼ�����󲻺�ָ��
    goods(char pname[],char pprice[],char padress[],char pmill[],int pid,goods *next);//��ʼ������ָ��

    //��ñ������ݳ�Ա�ĺ���
    char *getname();
    char *getprice();
    char *getadress();
    char *getmill();
    int   getid();

    goods *getnext();//��ýڵ�ָ��
    void  setnext(goods *next);//���ýڵ�ָ��
    void updatid();//id�Լ�

};
//���ж��庯����ʵ��

goods::goods()
{
    strcpy(name,"");
    strcpy(price,"");
    strcpy(adress,"");
    strcpy(mill,"");
    Mynext = NULL;
    id = 0;
}
goods::goods(char pname[],char pprice[],char padress[],char pmill[],int pid)
{
    strcpy(name,pname);
    strcpy(price,pprice);
    strcpy(adress,padress);
    strcpy(mill,pmill);
    Mynext = NULL;
    id = pid;
}
goods::goods(char pname[],char pprice[],char padress[],char pmill[],int pid,goods *next)
{
    strcpy(name,pname);
    strcpy(price,pprice);
    strcpy(adress,padress);
    strcpy(mill,pmill);
    Mynext = next;
    id = pid;
}
char *goods::getname()
{
    return name;
}
char *goods::getprice()
{
    return price;
}
char *goods::getadress()
{
    return adress;
}
char *goods::getmill()
{
    return mill;
}
goods *goods::getnext()
{
    return Mynext;
}
void goods::setnext(goods *next)
{
    Mynext = next;
}
int goods::getid()
{
    return id;
}
void goods::updatid()
{

    id = id - 1;
}

//������Ʒ����
class AddressBook
{
private:
    goods *myfirst;
public:
    AddressBook();//��ʼ��
    AddressBook(char npname[],char npprice[],char npadress[],char npmill[],int npid);//��������
    ~AddressBook();
    void Show_Link();//�鿴
    void Add_Link();//����

    void Edi_Link();//�޸�
    void Del_Link();//ɾ��
    void Log_Out();//�˳�


    void outall();//��ӡ��ǰ��������


};
//���ж��庯����ʵ��
AddressBook::AddressBook()
{
    myfirst = NULL;
}
AddressBook::AddressBook(char npname[],char npprice[],char npadress[],char npmill[],int npid)
{
    myfirst = new goods(npname,npprice,npadress,npmill,npid);
}
AddressBook::~AddressBook()
{
    goods *next = myfirst,*temp;
    while(next != NULL)
    {
        temp = next;
        next = next->getnext();
        delete temp;

    }
    myfirst = NULL;
}
//�����ӡ��Ʒ����
void AddressBook::outall()
{
    int i=0;
    goods *head = myfirst;
    if(head == NULL)
    {
        cout<<"[Ŀǰ��������]"<<endl;
        cout<<endl;


        return;
    }
    cout<<setw(10)<<"��ƷID"<<setw(20)<<"��Ʒ����"<<setw(20)<<"��Ʒ�۸�"<<setw(20)<<"��Ʒ����"<<setw(20)<<"��������"<<endl;
    cout<<"____________________________________________________________________________________________"<<endl;
    while(head)
    {
        i++;
        cout<<"|"<<setw(10)<<head->getid()<<setw(20)<<head->getname()<<setw(20)<<head->getprice()<<setw(20)<<head->getadress()<<setw(20)<<head->getmill()<<"|"<<endl;

        head = head -> getnext();
    }
    cout<<"|__________________________________________________________________________________________|"<<endl;

    cout<<"�ܼ�"<<"-"<<i<<"-"<<"����Ʒ\n\n"<<endl;
    cout<<endl;
    return;
}
//������Ʒ����
void AddressBook::Add_Link()
{

    outall();//�����������
    int adid;


    char adname[60],adprice[20],adadress[60],admill[60],flag[5];


    strcpy(flag,"y\0");
    while(strcmp(flag,"y\0") == 0)
    {

        //��ʼ����
        goods *p = myfirst;
        goods *rpeat = myfirst;//�ж�¼���Ƿ��ظ�

        cout<<"=��ʼ������Ʒ��Ϣ=\n"<<endl;
        cout<<"��Ʒ����:"<<endl;
        cin>>adname;

        cout<<"��Ʒ�۸�:"<<endl;
        cin>>adprice;
        while(rpeat)
        {
            if(strcmp(adname,rpeat->getname()) == 0 || strcmp(adprice,rpeat->getprice()) == 0)
            {
                cout<<"��Ʒ:"<<adname<<"/�۸�:"<<adprice<<"�Ѵ��ڣ�"<<endl;
                system("pause");
                system("cls");
                return;
            }
            rpeat = rpeat -> getnext();
        }

        cout<<"������Ʒ����:"<<endl;
        cin>>adadress;

        cout<<"������������:"<<endl;
        cin>>admill;


        if(p == NULL)
        {
            adid = 1;
            myfirst = new goods(adname,adprice,adadress,admill,adid);
        }
        else
        {
            while(p->getnext() != NULL)
            {

                p = p->getnext();
                //������ƷID
                adid = p->getid() + 1;
            }

            p ->  setnext(new goods(adname,adprice,adadress,admill,adid,p->getnext())) ;

        }
        system("pause");
        system("cls");
        cout<<"����ɹ���"<<endl;
        cout<<"�ǡ�y����n������������Ʒ��"<<endl;

        cin>>flag;
        system("cls");

    }



}

//�޸ı༭��Ʒ����
void AddressBook::Edi_Link()
{
    int n;//����
    char ename[60];
    outall();//���������Ʒ��Ϣ
    cout<<"������Ҫ�༭����Ʒ����:"<<endl;
    cin>>ename;
    system("cls");
    goods *head = myfirst;
    int flag = 0;
    while(head)
    {
        if(flag == 2)//�޸�����������ٱ���
        {
            break;
        }

        if(strcmp(head->getname(), ename)==0)
        {

            cout<<"Old-Data:"<<endl;
            cout<<setw(10)<<head->getid()<<setw(20)<<head->getname()<<setw(20)<<head->getprice()<<setw(20)<<head->getadress()<<setw(20)<<head->getmill()<<endl;

            while(1)
            {
                int i;
                char enew[60];
                cout<<"     +-----------------------------------+"<<endl;
                cout<<"     |   [�����ѡ����Ҫ�޸ĵ�����/�˳�] |"<<endl;
                cout<<"     |                                   |"<<endl;
                cout<<"     |              - 1.��Ʒ����         |"<<endl;
                cout<<"     |              - 2.��Ʒ�۸�         |"<<endl;
                cout<<"     |              - 3.��Ʒ����         |"<<endl;
                cout<<"     |              - 4.��������         |"<<endl;
                cout<<"     |              - 0.�˳�             |"<<endl;
                cout<<"     +-----------------------------------+"<<endl;
                cout<<".";
                cin>>i;
                if(i > 4 || i < 0)
                {
                    cout<<"���������������..."<<endl;
                    system("pause");
                    system("cls");
                    continue;
                }
                if(i == 0)
                {
                    system("cls");
                    break;
                }
                switch(i)
                {
                case 1:
                    cout<<"��Ʒ�����޸�Ϊ:"<<endl;
                    cin>>enew;
                    strcpy(head->getname(),enew);
                    system("cls");
                    break;
                case 2:
                    cout<<"�¼۸�:"<<endl;
                    cin>>enew;
                    strcpy(head->getprice(),enew);
                    system("cls");
                    break;
                case 3:
                    cout<<"�²���:"<<endl;
                    cin>>enew;
                    strcpy(head->getadress(),enew);
                    system("cls");
                    break;
                case 4:
                    cout<<"�²��� :"<<endl;
                    cin>>enew;
                    strcpy(head->getmill(),enew);
                    system("cls");
                    break;
                }
                //���޸ĵ�Ϊ��һ����Ʒʱ
                if(flag == 0)
                {
                    myfirst = head;
                }
                flag = 2;//��״̬�����޸Ĳ������
            }


        }
        else
        {
            flag = 1;//����head�Ѿ����ǵ�һ���ڵ���
            head = head -> getnext();
        }

    }
    if(flag != 2)
    {
        cout<<"�㻹δ�����κ���Ʒ��"<<endl;
        system("pause");
        system("cls");
    }
    else
    {
        system("cls");
        cout<<"New-Data:"<<endl;
        cout<<setw(10)<<head->getid()<<setw(20)<<head->getname()<<setw(20)<<head->getprice()<<setw(20)<<head->getadress()<<setw(20)<<head->getmill()<<endl;
        cout<<"__________"<<endl;
        cout<<ename<<"�޸ĳɹ���"<<endl;
        system("pause");
        system("cls");
    }
    return;
}
//ɾ���¼���Ʒ����
void AddressBook::Del_Link()
{
    char dname[60];
    outall();//���������Ʒ
    cout<<"������Ҫ�¼ܵ���Ʒ����:"<<endl;
    cin>>dname;
    goods *head = myfirst;
    goods *fhead = myfirst;//fhead����һ��ָ��head��ǰһ��
    int flag = 0;

    while(head)
    {

        if(strcmp(head->getname(), dname)==0)
        {
            cout<<"[DelData]:"<<endl;
            cout<<setw(10)<<head->getid()<<setw(20)<<head->getname()<<setw(20)<<head->getprice()<<setw(20)<<head->getadress()<<setw(20)<<head->getmill()<<endl;

            fhead->setnext(head->getnext());//��head��ǰһ���ڵ�ָ��head�ĺ�һ���ڵ㣻
            head = NULL;

            fhead = fhead->getnext();//��fhead��ʼID�Լ�һ

            while(fhead)
            {
                //cout<<"����ѭ��-�Լ�"<<endl;
                fhead->updatid();
                fhead = fhead->getnext();
            }
            //��ɾ����Ϊ��һ����Ʒʱ
            if(flag == 0)
            {
                myfirst = myfirst -> getnext();
                cout<<dname<<"��Ʒ���¼ܣ�"<<endl;
                cout<<"__________"<<endl;
                system("pause");
                system("cls");
                return;
            }
        }
        else
        {
            flag = 1;
            fhead = head;
            head = head -> getnext();

        }

    }
    if(flag == 0)
    {
        cout<<"�������ݣ�"<<endl;
        system("pause");
        system("cls");
    }
    else
    {
        cout<<"__________"<<endl;
        cout<<dname<<"��Ʒ���¼ܣ�"<<endl;
        system("pause");
        system("cls");
    }

    return;
}


//������
int main()
{
    AddressBook call;
    cout<<setiosflags(ios::left);//��������

    //system("color 0F");//ԭζ
    system("color 06");//��
    //system("color 04");//��
    //system("color 05");//��
    while(1)
    {
        //���˵�
        cout<<"     +--------------------------------------------------+"<<endl;
        cout<<"     |              [��Ʒ����ϵͳ]                      |"<<endl;
        cout<<"     |                                                  |"<<endl;
        cout<<"     |              * 1.�鿴������Ʒ                    |"<<endl;
        cout<<"     |              * 2.��������Ʒ                      |"<<endl;
        cout<<"     |              * 3.�޸���Ʒ��Ϣ                    |"<<endl;
        cout<<"     |              * 4.�¼�ɾ����Ʒ                    |"<<endl;
        cout<<"     |                                                  |"<<endl;
        cout<<"     |              * 0.�˳�                            |"<<endl;
        cout<<"     +--------------------------------------------------+"<<endl;
        int i;
        cout<<".";
        cin>>i;
        switch(i)
        {
        case(1):
            system("cls");
            call.outall();
            break;
        case(2):
            system("cls");
            call.Add_Link();
            break;
        case(3):
            system("cls");
            call.Edi_Link();
            break;
        case(4):
            system("cls");
            call.Del_Link();
            break;
        case(0):
            cout<<"��л����ʹ�ã�ף��������죡Bye-Bye- ��^��^�� ��"<<endl;
            return 0;
        default:
            cout<<"������������������..."<<endl;
            system("pause");
            system("cls");
            continue;
        }
    }

}